package mb.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
    /*
     *  Input string (sData) = hhmmss
     *  Output string hh + sDelimiter + mm + sDelimiter ss 
     */
    public static String formatTime(String sDelimiter, String sData){
        if(sData.length() < 6) return sData;
        return sData.substring(0,2) 
                + sDelimiter + sData.substring(2,4) 
                + sDelimiter + sData.substring(4,6);
    }
   
    public static String getCurrentDateTime(){
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");

        Date now = new Date();

        String strDate = sdfDate.format(now);
        String strTime = sdfTime.format(now);
        return strDate + " " + strTime;
    }
    public static String getFileNamewithDateTime(){
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd_MM_yyyy");
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH_mm_ss");

        Date now = new Date();

        String strDate = sdfDate.format(now);
        String strTime = sdfTime.format(now);
        return strDate;
    }
    public static String getDateInYYYYMMDD(){
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
        Date now = new Date();
        String strDate = sdfDate.format(now);
        return strDate;
    }
    public static String getCurrentDate(String sFormat){
        SimpleDateFormat sdfDate = new SimpleDateFormat(sFormat);
        Date now = new Date();
        String strDate = sdfDate.format(now);
        return strDate;
    }
    public static String formatDouble(String sFormat, String sInput){
        if(sInput == null || sInput.trim().length() == 0) return "0.0";
        if(sInput.equals("0")) return "0.0";
        double dValue     = Double.parseDouble(sInput);
        DecimalFormat df = new DecimalFormat(sFormat);
        return df.format(dValue);                
    }
    public static String formatDouble(String sFormat, double dInput){
        DecimalFormat df = new DecimalFormat(sFormat);
        return df.format(dInput);                
    }
    public static String calculateColor(String sCeiling, String sdFloor, String sRef, String sCurrent){
        double dCeiling, dFloor, dRef, dCurrent;
        dCeiling = Double.parseDouble(sCeiling);
        dFloor   = Double.parseDouble(sdFloor);
        dRef     = Double.parseDouble(sRef);
        dCurrent      = Double.parseDouble(sCurrent);
        
        if(dCurrent == dCeiling){
            return "c";
        }
        else if (dCurrent > dRef){
            return "i";
        }
        else if (dCurrent == dRef){
            return "e";
        }
        else if(dCurrent == dFloor){
            return "f";
        }
        else return "d";
    }
    public static int parseTimeByHour(String sInput){
        try{
            int iLength = sInput.length();
            if(iLength == 5){
                //HMMSS
                return Integer.parseInt(sInput.substring(0,1));
            }
            else if(iLength == 6){
                //HHMMSS
                return Integer.parseInt(sInput.substring(0,2));
            }
            else if(iLength == 8){
                //HH:MM:SS
                return Integer.parseInt(sInput.substring(0,2));
            }
            else return 0;
        }
        catch(Exception ex){
            System.out.println("Error:" + ex.toString());
            return 0;
        }
    }
    public  static  int parseTimeByMinute(String sInput){
        try{
            int iLength = sInput.length();
            if(iLength == 5 || iLength == 6){
                //HMMSS
                return Integer.parseInt(sInput.substring(1,3));
            }
            else if(iLength == 8){
                //HH:MM:SS
                return Integer.parseInt(sInput.substring(3,5));
            }
            else return 0;
        }
        catch(Exception ex){
            System.out.println("Error:" + ex.toString());
            return 0;
        }
    }
    public  static int parseTimeBySecond(String sInput){
        try{
            int iLength = sInput.length();
            if(iLength == 5 || iLength == 6){
                //HMMSS
                return Integer.parseInt(sInput.substring(3,5));
            }
            else if(iLength == 8){
                //HH:MM:SS
                return Integer.parseInt(sInput.substring(6,8));
            }
            else return 0;
        }
        catch(Exception ex){
            System.out.println("Error:" + ex.toString());
            return 0;
        }
    }
    
    public static double getDouble(String ddd){
        double d = 0;
        try{
            d = Double.parseDouble(ddd);
        }
        catch(Exception ex){
            
        }
        return d;
    } 
    
    public static long getLong(String ll){
        long l = 0;
        try{
            l = Long.parseLong(ll);
        }
        catch(NumberFormatException ex){
            try{
                l = new Double(ll).longValue();
            }
            catch(Exception exx){
                l = 0;
            }
        }
        return l;
    }
    
    public static double produceFormattedDouble(String sFormat, String sInput){
        if(sInput == null || sInput.trim().length() == 0) return 0.0;
        if(sInput.equals("0")) return 0.0;
        double dValue     = Double.parseDouble(sInput);
        DecimalFormat df = new DecimalFormat(sFormat);
        String sResult= df.format(dValue);                
        return Double.parseDouble(sResult);
    }
}